package edu.neusoft.kongfanfei317.yuejian1.activity;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import edu.neusoft.kongfanfei317.yuejian1.R;
import edu.neusoft.kongfanfei317.yuejian1.bean.CollectBean;
import edu.neusoft.kongfanfei317.yuejian1.bean.MasterListBean;
import edu.neusoft.kongfanfei317.yuejian1.bean.RegisterBean;
import edu.neusoft.kongfanfei317.yuejian1.listener.TListener;
import edu.neusoft.kongfanfei317.yuejian1.model.CollectMasterModel;

public class MasterDetailsActivity extends BaseActivity {
    Button food_back,food_buy;
    TextView food_name,food_price,food_intro,food_type_id,food_comment;
    ImageView food_img;
    String foodname,foodprice,foodintro,foodcomment,foodimg,typeid;

    ImageButton food_collect;
    int flag1;
    String flag,user_id,food_id;

    TListener<MasterListBean> tListener = new TListener<MasterListBean>() {
        @Override
        public void onResponse(MasterListBean foodListBean) {

        }

        @Override
        public void onFail(String msg) {

        }
    };
    TListener<CollectBean> judgeListener = new TListener<CollectBean>() {
        @Override
        public void onResponse(CollectBean collectBean) {
            flag = collectBean.getCollected().toString();
            if (flag.equals("1")) {
                flag1 = 1;
                food_collect.setImageResource(R.drawable.ic_collect_y);
            }
            else {
                flag1=0;
                food_collect.setImageResource(R.drawable.ic_collect_n);
            }
        }

        @Override
        public void onFail(String msg) {

        }
    };
    TListener<RegisterBean> collectListListener = new TListener<RegisterBean>() {
        @Override
        public void onResponse(RegisterBean registerBean) {
            if(registerBean.getSuccess().equals("1")){
                if(flag1==1) {
                    food_collect.setImageResource(R.drawable.ic_collect_n);
                    flag1 = 0;
                }else{
                    food_collect.setImageResource(R.drawable.ic_collect_y);
                    flag1=1;
                }showToast("操作成功");

            }
            else{
                showToast("操作失败");
            }
        }

        @Override
        public void onFail(String msg) {

        }
    };

    @Override
    void initView() {
        setLayout_file(R.layout.activity_master_details);
        food_back = (Button)findViewById(R.id.food_back);
        food_buy = (Button)findViewById(R.id.food_buy);
        food_name = (TextView)findViewById(R.id.food_name);
        food_price = (TextView)findViewById(R.id.food_price);
        food_intro = (TextView)findViewById(R.id.food_intro);
        food_type_id = (TextView)findViewById(R.id.food_type_id);
        food_comment = (TextView)findViewById(R.id.food_comment);
        food_collect = (ImageButton)findViewById(R.id.food_collect);
        food_img = (ImageView)findViewById(R.id.food_img);
        user_id = getUser_id();


    }

    @Override
    void initEvent() {
        food_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
                showToast("返回菜品列表");
            }
        });
        food_buy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.putExtra("foodname",food_name.getText().toString());
                intent.putExtra("foodprice",food_price.getText().toString());
                intent.setClass(MasterDetailsActivity.this,MasterBuyActivity.class);
                setResult(RESULT_OK,intent);
                startActivity(intent);
                showToast("请填写信息");
            }
        });
        food_collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CollectMasterModel collectFoodModel = new CollectMasterModel();
                collectFoodModel.collect(user_id,food_id,collectListListener);
            }
        });
    }

    @Override
    void initData() {
        Intent intent=getIntent();
        food_id = intent.getStringExtra("foodid");
        foodname=intent.getStringExtra("foodname");
        foodprice=intent.getStringExtra("foodprice");
        foodintro = intent.getStringExtra("foodintro");
        foodimg = intent.getStringExtra("foodimg");
        foodcomment = intent.getStringExtra("foodcomment");
        typeid = intent.getStringExtra("typeid");
        Picasso.with(this).load(foodimg).into(food_img);
        food_name.setText(foodname);
        food_price.setText(foodprice);
        food_type_id.setText(typeid);
        food_intro.setText(foodintro);
        CollectMasterModel collectFoodModel = new CollectMasterModel();
        collectFoodModel.judge(user_id,food_id,"1",judgeListener);


    }

  /*  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_details);
    }*/
}
